## STUDY 2 DATA PREPARATION

## set working directory

library(dplyr)
library(tidyr)

metoo2 <- read.csv("study2_replication_raw.csv")

### DATA PREPROCESSING

## measures indexing (allegation, post-communication)
metoo2$resign_pre <- as.numeric(as.character(metoo2$resign1_4))
metoo2$resign_post <- as.numeric(as.character(metoo2$resign2_4))

metoo2$likely_pre <- as.numeric(as.character(metoo2$likely1_1))
metoo2$likely_post <- as.numeric(as.character(metoo2$likely2_1))

## measure baselines (from allegation stage)
metoo2$resign_base <- metoo2$resign_pre
metoo2$likely_base <- metoo2$likely_pre

## scandal condition
metoo2$scandal_sex[metoo2$scandal_sex == "4"] <- "assault"
metoo2$scandal_sex[metoo2$scandal_sex == "1"] <- "affair"
metoo2$scandal_sex <- as.factor(metoo2$scandal_sex)

## stack data on respondent-measurement stage level

metoo2_1 <- metoo2 %>% dplyr::select(ResponseId, respondent_party, scandal_sex,
                                       communication,
                                       likely_pre, resign_pre, resign_base, likely_base)
## allegation stage

metoo2_2 <- metoo2 %>% dplyr::select(ResponseId, respondent_party, scandal_sex,
                                       communication,
                                       likely_post, resign_post, resign_base, likely_base)

##post-communication stage

metoo2_long <- dplyr::bind_rows(metoo2_1, metoo2_2)

metoo2_long <- unite(metoo2_long, "resign", c(resign_pre, resign_post), 
na.rm = TRUE, remove = FALSE)

metoo2_long <- unite(metoo2_long, "likely", c(likely_pre, likely_post), 
                    na.rm = TRUE, remove = FALSE)


## treatment group indexing

metoo2_long$treatment <- NA
metoo2_long$treatment[!is.na(metoo2_long$resign_pre)] <- "control"
## in stacked data, if resign_pre (first, allegation stage) is not NA: allegation group (control)

metoo2_long$treatment[!is.na(metoo2_long$resign_post) & 
                       metoo2_long$communication == "denial"] <- "denial"
## in stacked data, if resign_post (seond, communication stage) is not NA and communication is 'denial': denial group

metoo2_long$treatment[!is.na(metoo2_long$resign_post) & 
                       metoo2_long$communication == "confession"] <- "apology"
## in stacked data, if resign_prost (second, communication stage) is not NA and communication is 'apology': apology group

metoo2_long$treatment <- as.factor(metoo2_long$treatment)


metoo2_long <- within(metoo2_long, treatment <- relevel(treatment, ref = "control")) ## set reference level to allegation

metoo2_long$resign <- as.numeric(metoo2_long$resign)
metoo2_long$likely <- as.numeric(metoo2_long$likely)

## final dataset for study 2 analysis
metoo2_long <- metoo2_long %>% dplyr::select(ResponseId, treatment, scandal_sex, resign, likely, resign_base, likely_base)
#write.csv(metoo2_long, "study2_replication_analysis.csv")
